package com.easylinkin.linkappapi.facerecognition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.facerecognition.entity.Personnel;
import com.easylinkin.linkappapi.facerecognition.mapper.PersonnelMapper;
import com.easylinkin.linkappapi.facerecognition.service.PersonnelService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PersonnelServiceImpl extends ServiceImpl<PersonnelMapper, Personnel> implements PersonnelService {

    @Resource
    LinkappUserContextProducer linkappUserContextProducer;

    @Override
    public Personnel queryWithPersonnelName(String personnelName) {
        QueryWrapper<Personnel> qw = new QueryWrapper<>();
        qw.eq("name", personnelName);
        qw.eq("tenant_id", linkappUserContextProducer.getNotNullCurrent().getTenantId());

        return baseMapper.selectOne(qw);
    }

    @Override
    public Personnel queryPersonnelBySubjectId(String subjectId) {
        QueryWrapper<Personnel> qw = new QueryWrapper<>();
        qw.eq("subject_id", subjectId);

        return baseMapper.selectOne(qw);
    }

    @Override
    public IPage<Personnel> queryPersonnelList(Page<Personnel> page, Personnel personnel) {
        QueryWrapper<Personnel> qw = new QueryWrapper<>();
        qw.eq("tenant_id", linkappUserContextProducer.getNotNullCurrent().getTenantId());
        if (personnel.getName() != null && !personnel.getName().trim().isEmpty()) {
            qw.like("name", "%" + personnel.getName().trim() + "%");
        }

        if (personnel.getIdentifierId() != null) {
            qw.eq("identifier_id", personnel.getIdentifierId());
        }
        qw.orderByDesc("create_time");
        return baseMapper.selectPage(page, qw);
    }

    @Override
    public List<Personnel> listByIdentifierIds(List<Long> identifierIds) {
        QueryWrapper<Personnel> qw = new QueryWrapper<>();
        qw.in("identifier_id", identifierIds);
        qw.eq("tenant_id", linkappUserContextProducer.getNotNullCurrent().getTenantId());

        return baseMapper.selectList(qw);
    }
}
